/*
 * @Author: hongbin
 * @Date: 2023-03-30 14:32:10
 * @LastEditors: hongbin
 * @LastEditTime: 2023-03-30 15:58:00
 * @Description:
 */
import * as THREE from "three";
import vertexShader from "./vt.glsl";
import fragmentShader from "./fm.glsl";

export class PictureParticle {
    particle: THREE.Points<THREE.BufferGeometry, THREE.ShaderMaterial>;

    constructor(map: THREE.Texture, toMap: THREE.Texture) {
        const planeGeometry = new THREE.PlaneGeometry(8, 8, 300, 300);

        const { count } = planeGeometry.getAttribute("position");

        const toPosition = new Float32Array(count * 3);
        const { random } = Math;
        for (let index = 0; index < count; index++) {
            const i3 = index * 3;

            toPosition[i3] = (0.5 - random()) * 150;
            toPosition[i3 + 1] = (0.01 - random()) * 150;
            toPosition[i3 + 2] = (0.2 - random()) * 150;
        }

        planeGeometry.setAttribute(
            "toPosition",
            new THREE.BufferAttribute(toPosition, 3)
        );

        const particle = new THREE.Points(
            planeGeometry,
            new THREE.ShaderMaterial({
                vertexShader,
                fragmentShader,
                uniforms: {
                    map: { value: map },
                    toMap: { value: toMap },
                    progress: { value: 0 },
                },
                // transparent: true,
            })
        );

        this.particle = particle;
    }

    setProgress(progress: number) {
        this.particle.material.uniforms.progress.value = progress;
    }
}
